package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.constant.ProductConstant;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.atguigu.gulimall.product.vo.GroupRespVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationService relationService;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> qw = new QueryWrapper<>();
        qw.eq("1", "1");

        if (catelogId != 0) {
            qw.lambda().eq(AttrGroupEntity::getCatelogId, catelogId);
        }
        String key = (String) params.get("key");
        //notBlank 不能为null,并且不能为空字符串，并且trim()之后，不能为空字符串
        if (StringUtils.isNotBlank(key)) {
            qw.lambda().and(wrapper -> {
                wrapper.eq(AttrGroupEntity::getAttrGroupId, key);
                wrapper.or();
                wrapper.like(AttrGroupEntity::getAttrGroupName, key);
            });
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                qw
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> relationEntityList = this.relationService.list(
                new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .lambda()
                        .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
        );
        if (relationEntityList != null && relationEntityList.size() > 0) {
            List<Long> attrIds = relationEntityList.stream().map((relationEntity) -> {
                return relationEntity.getAttrId();
            }).collect(Collectors.toList());


            Collection<AttrEntity> attrEntities = attrService.listByIds(attrIds);
            return (List<AttrEntity>) attrEntities;
        } else {
            return null;
        }
    }

    /*
    * //传入一个attrGroupId,获取这个分组所属三级分类下未被分组的attr
    * */
    @Override
    public PageUtils getNoAttrRelations(Map<String, Object> params, Long attrGroupId) {

        String key = (String) params.get("key");
        //1、找到groupEntity
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        //2、根据groupEntity.catelogId找到所有分组groupEntities
        List<AttrGroupEntity> attrGroupEntityList = this.list(
                new QueryWrapper<AttrGroupEntity>()
                .lambda()
                .eq(AttrGroupEntity::getCatelogId,attrGroupEntity.getCatelogId())
        );
        List<Long> attrGroupIdList = attrGroupEntityList.stream().map((groupEntity) -> {
            return groupEntity.getAttrGroupId();
        }).collect(Collectors.toList());

        //3、获取所有已分配组的groupedAttrIdList
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList = relationService.list(
                new QueryWrapper<AttrAttrgroupRelationEntity>()
                .lambda()
                .in(AttrAttrgroupRelationEntity::getAttrGroupId,attrGroupIdList)
        );
        List<Long> groupedAttrIdList = attrAttrgroupRelationEntityList.stream().map(relationEntity -> {
            return relationEntity.getAttrId();
        }).collect(Collectors.toList());
        //4、根据groupEntity.catelogId,groupedAttrEntities找到所有attrEntities
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(AttrEntity::getCatelogId,attrGroupEntity.getCatelogId())
                .eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode())
                .notIn(AttrEntity::getAttrId,groupedAttrIdList);
        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<GroupRespVo> getListWithAttr(Long catelogId) {
        //1、根据catelogId查询所有分组
        List<AttrGroupEntity> groupEntityList = this.list(
                new QueryWrapper<AttrGroupEntity>()
                        .lambda()
                        .eq(AttrGroupEntity::getCatelogId, catelogId)
        );
        //2、给每个分组设置属性集合
        List<GroupRespVo> groupRespVoList = groupEntityList.stream().map((entity) -> {
            GroupRespVo vo = new GroupRespVo();
            BeanUtils.copyProperties(entity, vo);
            List<AttrAttrgroupRelationEntity> relationEntityList = relationService.list(
                    new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .lambda()
                            .eq(AttrAttrgroupRelationEntity::getAttrGroupId, entity.getAttrGroupId())
            );
            if(relationEntityList != null && relationEntityList.size() > 0) {
                List<Long> attrIdList = relationEntityList.stream().map((relationEntity) -> {
                    return relationEntity.getAttrId();
                }).collect(Collectors.toList());

                List<AttrEntity> attrEntityList = (List<AttrEntity>) attrService.listByIds(attrIdList);
                List<AttrVo> attrVoList = attrEntityList.stream().map((attrEntity) -> {
                    AttrVo attrVo = new AttrVo();
                    BeanUtils.copyProperties(attrEntity, attrVo);
                    return attrVo;
                }).collect(Collectors.toList());

                vo.setAttrs(attrVoList);
            }else{
                vo.setAttrs(new ArrayList<>());
            }
            return vo;
        }).collect(Collectors.toList());
        return groupRespVoList;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、当前spu有多少对应的属性分组
        List<SpuItemAttrGroupVo> vos = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }

}